
import os, sys, io, re, json, thread, time, logging
import pyxul

logging.basicConfig(level=logging.DEBUG)

def action_delay_notify(param):
	def worker(a, b):
		time.sleep(3)
		pyxul.sendmsg(u'xul', u'')
	thread.start_new_thread(worker, (1, 2))
	
def action_throw_exception(param):
	raise BaseException(u'an exception')


def message_receiver(title, message):
	logging.info('recv: title=%s; message=%s' % (title, message))
	data = json.loads(message)
	action_name = data['action']
	if not type(action_name) == unicode:
		raise BaseException(u'action_name must be unicode')
	action = eval('action_' + action_name)
	if action == None:
		raise BaseException(u'can not find action')
	action(data)
	
	
pyxul.add_receiver(u'python', message_receiver)
logging.info('init ok')